package activator;

import library.service.Book;
import library.service.LibraryService;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

public class Activator implements BundleActivator {

	private ServiceReference[] serviceRefs;
	private static final int LIBRARY_SERVICES_COUNT = 2;
	private static int counter = 1;

	public void start(BundleContext context) throws Exception {
		System.out
				.println("CONSUMER ServiceFactory:  START() method, counter= "
						+ counter);

		for (; counter <= LIBRARY_SERVICES_COUNT; counter++) {
			// String filter = "(counter=" + 2 + ")";
			// System.out.println("filter: " + filter);
			// ServiceReference[] tempServiceRefs =
			// context.getServiceReferences(
			// LibraryService.class.getName(), filter);

			System.out.println("CONSUMER ServiceFactory: counter= " + counter);

			ServiceReference[] serviceRefsArray = context
					.getServiceReferences(LibraryService.class.getName(),
							"(counter=" + counter + ")");
			if (serviceRefsArray != null && serviceRefsArray.length ==1) {
				ServiceReference serRef = serviceRefsArray[0];
				System.out.println("CONSUMER ServiceFactory: " + counter
						+ "  ServiceRef= " + serRef);
			} else {
				System.out
						.println("CONSUMER ServiceFactory: ServiceReference NOT FOUND");
			}

			// if (sr != null) {
			// System.out.println(((Integer) sr.getProperty("counter"))
			// .intValue());
			// System.out.println("sr != null");
			// } else {
			// System.out.println("sr == null");
			// }

			// if (tempServiceRefs != null) {
			// serviceRefs[i] = tempServiceRefs[0];
			// System.out.println("tempServiceRefs != null");
			// } else {
			// System.out.println("tempServiceRefs == null");
			// }
			// if (serviceRefs[i] != null) {
			//
			// // 1. serviceCounter stuff
			// Integer serviceCounterInteger = (Integer) serviceRefs[i]
			// .getProperty("counter");
			// if (serviceCounterInteger != null) {
			// int serviceCounter = serviceCounterInteger.intValue();
			// System.out
			// .println("CONSUMER ServiceFactory:  serviceCounter= "
			// + serviceCounter);
			// }
			//
			// // 2. load and consume LibraryService
			// LibraryService libraryService = (LibraryService) context
			// .getService(serviceRefs[i]);
			// Book book = null;
			// if (libraryService != null) {
			// book = libraryService.findBookByName("");
			// }
			//
			// // 3. print data
			// if (book != null) {
			// System.out.println("Found book: " + book.getName());
			// } else {
			// System.out.println("NOT Found book: " + book.getName());
			// }
			//
			// }
			System.out.println("---------");
		}

	}

	public void stop(BundleContext context) throws Exception {

		if (serviceRefs != null) {
			System.out.println("CONSUMER ServiceFactory:  STOP() method.");

			for (int i = 0; i < serviceRefs.length; i++) {
				if (serviceRefs[i] != null) {
					context.ungetService(serviceRefs[i]);
				}
			}
			serviceRefs = null;
		}
	}
}
